
const db = require('../db/echarts.js')

exports.save_currentEchartsList = (req,res)=>{
  const req_body = req.body
  console.log('req.body',req.body)
  定义数据库查询语句
  const sqlInsertCanvas = 'insert into ev_echart_canvas set name = ?'
  const sqlStr = 'select * from ev_echart_config where _id = ?'
  db.query(sqlInsertCanvas,req_body.canvasName,(err,saveCanvasRes)=>{
    if(err) return res.cc(err)

    db.query(sqlStr,req_body.id,(err,results)=>{
      if(err) return res.cc(err)
  
      if(results.length>0) return res.cc()
      const sqlInsert = 'insert into ev_echart_config set ?'
      const sqlInsertOpt = 'insert into ev_echart_config_option set ?'
      
      db.query(sqlInsert,
        {
          // option_id:req_body.id,
          type:req_body.type,
          x:req_body.x,
          y:req_body.y,
          width:req_body.width,
          height:req_body.height,
          canvas_id:saveCanvasRes.insertId},(err,results)=>{
            // console.log("req_body",req_body.option.backgroundColor)
            if(err) return res.cc(err)
            if(results.affectedRows !==1) return res.cc('保存失败')
            db.query(sqlInsertOpt,{
            
              backgroundColor:req_body.option.backgroundColor,
              titile_text:req_body.option.title.text,
              xAxis:JSON.stringify(req_body.option.xAxis.data),
              color:JSON.stringify(req_body.option.color),
              
              yAxis:JSON.stringify(req_body.option.yAxis.data),
              _id:results.insertId,
            },(err,results)=>{
              if(err) return res.cc(err)
              if(results.affectedRows !==1) return res.cc('保存失败')
              res.cc("保存成功")
            })
            
          })
    })
  })


  
}